package ca.etsmtl.log430.lab3;

/**
 * This class is used by various other classes that need to keep a list of
 * students on hand. It provides the basic functionality for storage and
 * retrieval of the Student Object from the list.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class StudentList extends List {

	/**
	 * Adds a new student to the list. All the issues of casting are taken care
	 * of within this class.
	 * 
	 * @param studentObject
	 */
	public void addStudent(Student studentObject)	{
		appendItemToList((Object) studentObject);
	}

	/**
	 * Returns the student pointed at the current position pointed to by the
	 * internal list pointer of the internal list. Subsequent calls will return
	 * the next student object in the list. A null object is returned if list is
	 * empty or the end of list has been reached.
	 * 
	 * @return the current Student if found, null otherwise.
	 */
	public Student getNextStudent()	{
		return (Student) getItemFromList();
	}

	/**
	 * Determines whether the student is currently in the student list. Note
	 * that this method assumes that all students have different identification
	 * numbers.
	 * 
	 * @param studentObject
	 * @return true if the student is in the list, false otherwise.
	 */
	public boolean isStudentInList(Student studentObject) {
		Student currentObject;
		boolean done = false;
		boolean result = false;

		goToFrontOfList();

		while (!done) {
			currentObject = getNextStudent();

			if (currentObject == null) {
				done = true;
			} else {
				if (studentObject.getStudentID().compareTo( currentObject.getStudentID()) == 0) {
					result = true;
				}
			}
		}
		return(result);
	}

	/**
	 * Finds a student in a list using the StudentID as the search key. Note
	 * that this method assumes that all courses have different identification
	 * numbers.
	 * 
	 * @param studentID
	 * @return a Student if studentID if found in list, null otherwise.
	 */
	public Student findStudentByID(String studentID) {
		Student currentObject = null;
		boolean done = false;
		boolean found = false;

		goToFrontOfList();

		while (!done) {
			currentObject = getNextStudent();

			if (currentObject == null) {
				done = true;
			} else {
				if (currentObject.getStudentID().compareTo(studentID) == 0 ) {
					found = true;
					done = true;
				}
			}
		}

		if (found) { 
			return(currentObject);
		} else {
			return(null);
		}
	}
}